/*
  Name: Client.h
  Copyright: 
  Author: Carl Chatfield
  Date: 06/01/08 12:57
  Description: Stores client info as well as threads for each module
*/
#ifndef __DEADPROXYCLIENT_h__
#define __DEADPROXYCLIENT_h__

#ifdef __BORLANDC__
	#pragma hdrstop
#endif

#ifndef WX_PRECOMP
	#include <wx/wx.h>
#else
	#include <wx/wxprec.h>
#endif

#include "DeadProxyPacketBuffer.h"
#include "DeadProxyTibiaRW.h"
#include "DeadProxyPacketLog.h"
#include "DeadProxyClientEventHandler.h"
#include "DeadProxyClientSendQueue.h"
#include "DeadProxyClientNetworkHandler.h"
#include "DeadProxyProtocol.h"
#include "DeadProxyClientMap.h"
#include "DeadProxyClientPlayer.h"
#include "DeadProxyClientVIP.h"
#include "DeadProxyClientContainer.h"
#include "DeadProxyClientMessageLog.h"

class Client
{
    private:
        char key[16];
        
        int cpid;
        int cport;
        void* chandle;
        wxString CharacterName;
        wxString WorldName;
        bool isSelected;
        CharacterList * pCharacterList;
        bool standAlone; //dont worry wont be implemented for a while yet
        
        //Protocol
        Protocol * Proto;
        
        //GUI access variables
        int logPacketMode;
        PacketLog * PackLog;
        
        int LogMessageChannel;
        
        //Utils
        TibiaRW * RW;
        
        //Event Handling Stuff
        EventQueue * EvtQueue;
        EventHandler * EvtHandler;
        wxCondition * EvtCondition;
        wxMutex * EvtMutex;
        
        //Send Queue Stuff
//        PacketQueue * PackQueue;
//        SendQueue * SndQueue;
//        wxMutex * SendMutex;
//        wxCondition * SendCondition;
//        wxMutex * PacketMutex;
//        wxCondition * PacketCondition;
        //Networking stuff
        ClientNetworkHandler * Network;
        
        //Destory Self is called by the event handler
        void DestorySelf();
        
    public:
        //Init Client Selection Info
        struct InitSelectInfo
        {
            wxString * CharName;
            wxString * WorldName;
            int pid;
            int port;
            bool IsStandAlone;
            int LogPacketMode;
            wxArrayString * PacketSelectListBoxArray;
        };
            
        //Game Info Stuff
        GameMap * Map;
        PlayerInfo * Player;
        VIPList * VIP;
        ContainerManager * Containers;
        MessageLog * MLog;
        
        
        //these are constant, can only get values
        int GetPid();
        void* GetHandle();
        bool IsStandAlone();
        //these can set and get
        int GetPort();
        void SetPort(int port);
        char* GetKey();
        wxString GetCharName();
        void SetCharName(wxString name);
        
        void SetSelected(ClientEvent * evt);
        //Packet Function Wrappers
        void SetPacketString(ClientEvent * evt);

        
        wxString * GetPacketString(ClientEvent * evt);
        void ClearPacketLog();
        wxArrayString * GetPacketListBoxArray();
        void SetLogPacketMode(ClientEvent * evt);
        int GetLogPacketMode();
        
        //Message Log Wrappers
        void AddMessage(char * sender, char * text, int channelid, int type);
        void OpenChannel(char * channelname, int channelid);
        void ClearMessageLog(ClientEvent * evt);
        void SetMessageBoxArray(ClientEvent * evt);
        void SetMessageInfo(ClientEvent * evt);
        void SetLogMessageChannel(ClientEvent * evt);
        
        //Event Handler Stuff
        void PostEvent(ClientEvent* evt);
        void EventParser(ClientEvent* evt);
        //Packet Parsers
        void ParseSendPacket(PacketBuffer * packet);
        void ParseRecvPacket(PacketBuffer * packet);
        
        //New Parse functions
        void ParseLoginPacket(ClientEvent * evt);
        void ParseGamePacket(ClientEvent * evt);
        
        //TibiaRW functions, try not to overuse these if you can avoid it
        int GetConnectionStatus();
        void GetConnectingCharacterInfo(int * ip, short * port);
        
        //Handover functions, called from an outside thread to pass control over to the client
        void PassOverLogin(PacketBuffer * packet);
        void PassOverGame(int clientSock, int serverSock);
        
        void TestFunc();
        Client(int pid, void* handle, int port, wxString name, bool isStandAlone);
        virtual ~Client();
};

#endif
